<?php

namespace App\Http\Requests;

use App\User;
use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name'=>'required|unique:users',
            'password'=>'required|min:5|confirmed',
            'account'=>[
                'required',
                function($attribute,$value,$fail){
                    if(filter_var($value,FILTER_VALIDATE_EMAIL)){
                        $user=User::where('email',$value)->first();
                        if($user){
                            return $fail('该邮箱已注册');
                        }
                        return true;
                    }else{
                        $user=User::where('mobile',$value)->first();
                        if($user){
                            return $fail('该手机号已注册');
                        }
                        return true;
                    }
                }
            ],
//            'code'=>[
//                'required',
//                function($attribute,$value,$fail){
//                    if($value!=session('vail_date.code')){
//                        return $fail('验证码不正确');
//                    }
//                    return true;
//                }
//            ]
        ];
    }
    public function messages()
    {
        return [
            'name.required'=>'用户名不能为空',
            'name.unique'=>'用户名已存在',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能小于5位',
            'password.confirm'=>'两次密码输入不一致',
            'account.required'=>'账号不能为空',
            'code.required'=>'验证码不能为空'
        ];
    }
}
